import socket
import threading
import time

def doPing(scoket):
## 基础数据
    telLen = 0x2E
    telId = '122001' #装钢偏斜30.4
    planNo = '120330000000'
    doorNum=0x3 # 装钢偏斜炉门编号
    spare = 0x3

    ## 数据转换为bytes
    telLenByte = telLen.to_bytes(2,'big')
    telIdByte = telId.encode()
    planNoByte= planNo.encode()
    doorNumByte=doorNum.to_bytes(2,'big')
    spareByte = spare.to_bytes(24,'big')


    s.sendall(telLenByte + telIdByte + planNoByte + doorNumByte+ spareByte)
    print("--------- socket已发送 -----------")
    data_rec=s.recv(102400)
    print("--------- 接收到服务端推送的数据 -----------")
    print(data_rec.decode('utf-8'))
def ping(scoket):
    while True:
        doPing(scoket)
        time.sleep(30)

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(("127.0.0.1",55201))
s.connect(("127.0.0.1",5001))

# doPing(s)
# time.sleep(1)
# s.close()
# 创建一个线程专门接收服务端数据并且打印
t1 = threading.Thread(target=ping,args=(s,))
t1.start()

